--
-- Author: ping.wang
-- Date: 2018-07-10 14:47:47
--
-- AuctionProductItemRender
--
local IconRender = require("app.widget.renders.IconRender")
local ClassRef = app.mvc.createListViewCell(...)
local TimeUtil_secondsToTime = TimeUtil.secondsToTime
local string_format = string.format
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AuctionProductItem))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._panel_1 = self._ui.Panel_1 --普通商品
    self._panel_2 = self._ui.Panel_2 --竞价商品

    self._normalProductName = self._panel_1:findChild("name") --普通商品的名字
    self._normalProductNum = self._panel_1:findChild("num1") --普通商品的数量
    self._normalProductPrice = self._panel_1:findChild("num_2/num") --普通商品的价格 单价

    local coinId = tonumber(GD:getSystemPar( "auctionNormalGoodsLootType",99))
    local coinConfig = GD:getCoinConfig(coinId)
    local iconPath = string.format("%s%s.png",Res.coinIconPath,coinConfig.bigicon)
    self._normalGoldIcon = self._panel_1:findChild("num_2/gold") --普通商品的金币
    self._normalGoldIcon:loadTexture(iconPath)


    self._bidProductName = self._panel_2:findChild("name") --竞价商品的名字
    self._bidProductFixedPrice = self._panel_2:findChild("num_fixed/num") --竞价商品一口价
    self._bidProductCurrentPrice = self._panel_2:findChild("num_1/num") --竞价商品的价格
    self._bidProductMyPricePanel = self._panel_2:findChild("num_2") --我的出价的panel
    self._bidProductMyPrice = self._panel_2:findChild("num_2/num") --竞价商品我的出价
    self._bidProductMyPriceGoldIcon = self._panel_2:findChild("num_2/gold") --竞价商品我的出价旁边的金币icon
    self._bidProductTips = self._panel_2:findChild("tips") --竞价商品 若是自己的 则显示N/A 若没出价 则显示 未出价
    self._bidProductFixedGoldIcon = self._panel_2:findChild("num_fixed/gold") --竞价商品一口价边的金币图标
    self._bidProductCurrentGoldIcon = self._panel_2:findChild("num_1/gold") --竞价商品当前价格边的金币图标

    self._bidPullPanel = self._panel_2:findChild("Panel_bar_2") --竞价显示的下架panel
    self._bidPullBar = self._panel_2:findChild("Panel_bar_2/LoadingBar") --竞价显示的下架的进度条
    self._bidPullText = self._panel_2:findChild("Panel_bar_2/num") --竞价显示的下架的提示  XX:XX:XX后下架


    local bidCoinId = 99
    local publisher = app:getInnerPublisher()
    if publisher == Enums.Publisher.tencent then
        bidCoinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType_tencent",99))
    else
        bidCoinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType",115))
    end

    local bidCoinConfig = GD:getCoinConfig(bidCoinId)
    local bidIconPath = string.format("%s%s.png",Res.coinIconPath,bidCoinConfig.bigicon)
    self._bidProductMyPriceGoldIcon:loadTexture(bidIconPath)
    self._bidProductFixedGoldIcon:loadTexture(bidIconPath)
    self._bidProductCurrentGoldIcon:loadTexture(bidIconPath)

    self._itemIcon = IconRender.create( self._ui.icon )
    self._itemIcon:setTipEnable( true )

    self._iconMy = self._ui.icon_my
    self._iconBuy = self._ui.icon_buy

    self._pullOffBtn = self._ui.Button_1 --下架
    self._offerBtn = self._ui.Button_2 --出价
    self._bidBtn = self._ui.Button_3 --加价

    self._pullOffBtn:addClickEventListener(function()
        local goodData = self._data
        if goodData then
            --下架指定商品
            self:dispatchCellEvent("pullOffItem", goodData)
        end
    end)

    self._offerBtn:addClickEventListener(function()
        local goodData = self._data
        if goodData then
            --都是弹出购买界面
            self:dispatchCellEvent("offerItem", goodData)
        end
    end)

    self._bidBtn:addClickEventListener(function()
        local goodData = self._data
        if goodData then
            --弹出购买界面
            self:dispatchCellEvent("offerItem", goodData)
        end
    end)


    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local goodData = self._data
        --dump(goodData)
        local itemData = goodData:getItemData()
        self._itemIcon:setData(itemData)
        if itemData:getAcType() == Enums.AuctionProductType.normal then
            self:_refreshNormalItem(goodData)
            self._itemIcon:setNumBgVisible(false)
        else
            self:_refreshBidItem(goodData)
            self._itemIcon:setNumBgVisible(true)
        end

    else
        self:setVisible( false )
    end
end

function ClassRef:_refreshNormalItem(goodData)
    self._panel_1:setVisible(true)
    self._panel_2:setVisible(false)
    local itemData = goodData:getItemData()
    self._normalProductName:setString(itemData:getName(true)) --普通商品的名字
    self._normalProductNum:setString(goodData:getCount()) --普通商品的数量
    self._normalProductPrice:setString(goodData:getSellPrice()) --普通商品的价格 单价
    self._iconBuy:setVisible(false)
    self._iconMy:setVisible(false)
    --购买按钮和出价按钮是一样的 只是文字描述不一样 判断是否是自己的商品 若是 则显示下架按钮 否则 显示购买按钮
    --auction_button2 购买 auction_button7 出价
    self._bidBtn:setVisible(false)
    if goodData:isMySellItem() then
        self._pullOffBtn:setVisible(true)
        self._offerBtn:setVisible(false)
        self._iconMy:setVisible(true)
    else
        self._pullOffBtn:setVisible(false)
        self._offerBtn:setVisible(true)
        self._offerBtn:getChildByName("name"):setString(L("auction_button2"))
    end
end

function ClassRef:_refreshBidItem(goodData)
    self._panel_1:setVisible(false)
    self._panel_2:setVisible(true)

    local itemData = goodData:getItemData()
    self._bidProductName:setString(itemData:getName(true)) --商品的名字
    self._bidProductFixedPrice:setString(string.format(L("auction_donePrice"),goodData:getFixedPrice()))
    self._bidProductCurrentPrice:setString(tostring(goodData:getBidingPrice()))

    self._bidProductMyPricePanel:setVisible(false)
    self._bidProductTips:setVisible(false)

    self._iconBuy:setVisible(false)
    self._iconMy:setVisible(false)

    if goodData:isMySellItem() then
        --出过价的竞价商品是不可以下架的
        local hasBiding = goodData:hasBiding() --竞价商品当前的竞价
        if hasBiding then
            self._pullOffBtn:setVisible(false)
        else
            self._pullOffBtn:setVisible(true)
        end

        self._offerBtn:setVisible(false)
        self._bidBtn:setVisible(false)
        self._bidProductTips:setVisible(true)
        self._bidProductTips:setString("N/A")
        self._iconMy:setVisible(true)
    else
        self._pullOffBtn:setVisible(false)
        --判断商品我是否出过价
        local myBiddingPrice = goodData:getMyBiddingPrice()
        if myBiddingPrice == 0 then
            --如果没有显示提示
            self._bidProductTips:setVisible(true)
            self._bidProductTips:setString(L("auction_notBid"))
            self._bidBtn:setVisible(false) --加价
            self._offerBtn:setVisible(true) --出价
            self._offerBtn:getChildByName("name"):setString(L("auction_button7")) --出价
        else
            --如果有显示我出售的价格
            self._iconBuy:setVisible(true)
            self._bidProductMyPricePanel:setVisible(true)
            self._bidProductMyPrice:setString(tostring(myBiddingPrice))
            self._bidBtn:setVisible(true) --加价按钮
            self._offerBtn:setVisible(false) --出价按钮
        end
    end

    self:_adjustIconPosX(self._bidProductFixedGoldIcon,self._bidProductFixedPrice)
    self:_adjustIconPosX(self._bidProductCurrentGoldIcon,self._bidProductCurrentPrice)
    self:_adjustIconPosX(self._bidProductMyPriceGoldIcon,self._bidProductMyPrice)

    self:stopAllActions()
    self:_refreshBidTimeProgressBar(goodData)
    self:schedule(function() self:_refreshBidTimeProgressBar(goodData) end,1)
end

--刷新竞价商品显示的进度条
function ClassRef:_refreshBidTimeProgressBar(goodData)
    local outSaleDurationTime = goodData:getOutSaleDurationTime()

    if outSaleDurationTime >= 0 then

        self._bidPullPanel:setVisible(true)
        self._bidPullBar:setVisible(true)
        self._bidPullText:setVisible(true)

        local timeStr = TimeUtil_secondsToTime(outSaleDurationTime)
        self._bidPullText:setString(string_format(L("text_12200013"),timeStr))
        self._bidPullBar:setPercent(goodData:getLastOnShelvesTimePecent(outSaleDurationTime))
    else
        --该商品需要下架 因为已经超过上架的时间了
        self:stopAllActions()
        self:dispatchCellEvent("localOffShelves", goodData:getProductUUId())
    end
end

--基于文本调整ICON的位置
function ClassRef:_adjustIconPosX(icon,text)
    local textAnchorPointX = text:getAnchorPoint().x
    local textPosX = text:getPositionX()
    local fixedGoldenPosX = textPosX + text:getContentSize().width * (1 - textAnchorPointX) + icon:getContentSize().width * 0.6
    icon:setPositionX(fixedGoldenPosX)
end

return ClassRef
